package com.onluy.frameworkcloud.batis.dialect;

public class OracleDialect implements Dialect {
    public String getPagingSql(String sql, int start, int end) {
        StringBuilder str = new StringBuilder();
        str.append("SELECT * FROM (SELECT ROW_.*, ROWNUM ROWNUM_ FROM (");
        str.append(sql);
        str.append(") ROW_ WHERE ROWNUM <= " + end + ") WHERE ROWNUM_ > " + start);
        return str.toString();
    }

    public String getCountSql(String sql) {
        return "SELECT COUNT(1) CNT FROM (" + sql + ")";
    }

    public String getToDate(String value) {
        return "TO_DATE(\'" + value + "\',\'" + this.getDateFormat(value) + "\')";
    }

    public String getDateFormat(String value) {
        switch (value.length()) {
            case 4 :
                return "YYYY";
            case 5 :
            case 9 :
            case 11 :
            case 12 :
            case 14 :
            case 15 :
            case 17 :
            case 18 :
            case 20 :
            default :
                return "";
            case 6 :
                return "YYYYMM";
            case 7 :
                return "YYYY-MM";
            case 8 :
                return "YYYYMMDD";
            case 10 :
                return "YYYY-MM-DD";
            case 13 :
                return "YYYY-MM-DD HH24";
            case 16 :
                return "YYYY-MM-DD HH24:MI";
            case 19 :
                return "YYYY-MM-DD HH24:MI:SS";
            case 21 :
                return "YYYY-MM-DD HH:MM:SS.S";
        }
    }
}